package com.kgc.scd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kgc.scd.bean.ShopSignInDay;
import com.kgc.scd.bean.ShopUser;
import com.kgc.scd.constant.SignInConstant;
import com.kgc.scd.mapper.ShopSignInDayMapper;
import com.kgc.scd.mapper.ShopUserMapper;
import com.kgc.scd.service.SignInService;
import com.kgc.scd.util.SnowUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created On : 2022/11/17.
 * <p>
 * Author : yantao
 * <p>
 * Description: SignInServiceImpl
 */
@Slf4j
@Service
public class SignInServiceImpl implements SignInService {
    @Autowired
    private ShopUserMapper shopUserMapper;

    @Autowired
    private ShopSignInDayMapper shopSignInDayMapper;

    @Autowired
    private SnowUtil snowUtil;

    /**
     * @author : yantao
     * @date   : 2022/11/17
     * @param  : [userId]
     * @return : void
     * @description : 添加
     */
    @Override
    public void addLargeIntegral(String userId) {
        ShopUser shopUser= shopUserMapper.selectById(userId);
        Integer i= shopUser.getIntegration();
        shopUser.setIntegration(i + SignInConstant.USER_SIGN_LARGE_INTEGRAL);
        QueryWrapper<ShopUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",shopUser.getId());
        shopUserMapper.update(shopUser,queryWrapper);
    }

    @Override
    public void addSmallIntegral(String userId) {

        ShopUser shopUser= shopUserMapper.selectById(userId);
        Integer i= shopUser.getIntegration();
        shopUser.setIntegration(i + SignInConstant.USER_SIGN_SMALL_INTEGRAL);
        QueryWrapper<ShopUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",shopUser.getId());
        shopUserMapper.update(shopUser,queryWrapper);
    }

    @Override
    public void recordTime(String userId) {
        ShopSignInDay shopSignInDay = new ShopSignInDay();

        shopSignInDay.setDay(new Date());
        shopSignInDay.setUserId(userId);
        shopSignInDay.setId(snowUtil.snowId());

        shopSignInDayMapper.insert(shopSignInDay);
    }

    @Override
    public List<ShopSignInDay> selectDays(String userId) {
        QueryWrapper<ShopSignInDay> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);


        return shopSignInDayMapper.selectList(queryWrapper);
    }
}
